// https://leetcode.cn/problems/palindrome-partitioning/
// Created by ade on 2022/7/26.
// 给你一个字符串 s，请你将 s 分割成一些子串，使每个子串都是 回文串 。返回 s 所有可能的分割方案。
// 回文串 是正着读和反着读都一样的字符串。
// 输入：s = "aab"
// 输出：[["a","a","b"],["aa","b"]]
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    int len = 0;
    vector <vector<int>> cache;
    vector <vector<string>> res;
    vector <string> tmp;

    vector <vector<string>> partition(string s) {
        len = s.size();
        cache.assign(len, vector<int>(len));
        dfs(s, 0);
        return res;
    }

    void dfs(const string &s, int start) {
        if(start == len){
            res.push_back(tmp);
            return;
        }
        for (int i = start; i < len; i++) {
            if(isPalindrom(s, start, i)){
                tmp.push_back(s.substr(start, i - start + 1));
                dfs(s, i + 1);
                tmp.pop_back();
            }
        }
    }

    bool isPalindrom(const string &s, int left, int right) {
        if (left == right || left == right - 1) {
            if (s[left] == s[right]) {
                cache[left][right] = 1;
                return true;
            } else {
                cache[left][right] = -1;
                return false;
            }
        }
        if (s[left] == s[right] && isPalindrom(s, left + 1, right - 1) == 1) {
            cache[left][right] = 1;
            return true;
        } else {
            cache[left][right] = -1;
            return false;
        }
    }
};

int main() {
    string s = "aab";
    Solution so;
    auto a = so.partition(s);
    for (auto i : a) {
        for (auto j : i) {
            cout << j << ",";
        }
        cout << endl;
    }
    return 0;
}